package com.atguigu.srb.core.controller.admin;


import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.UserInfoPageVO;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.result.ResponseVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2024-04-15
 */
@RestController
@RequestMapping("/admin/core/userInfo")
@Api(tags = "管理员操作的用户接口")
public class AdminUserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("分页查询")
    @GetMapping("list/{pageNum}/{pageSize}")
    public ResponseVo page(UserInfoPageVO userInfoPageVO, @PathVariable Integer pageNum,
                           @PathVariable Integer pageSize) {
        Page<UserInfo> userInfoPage = new Page<>(pageNum, pageSize);
        userInfoService.selectPage(userInfoPageVO, userInfoPage);
        return ResponseVo.ok().addData("item", userInfoPage);
    }

    @ApiOperation("切换用户的状态")
    @PutMapping("{userId}/{status}")
    public ResponseVo changeUserStatus(@PathVariable Long userId, @PathVariable Integer status) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        if(status == 0){
            userInfo.setStatus(1);
        }else if(status == 1) {
            userInfo.setStatus(0);
        }
        userInfoService.updateById(userInfo); // 部分更新，非空字段会进行更新操作
        return ResponseVo.ok().message(status==0?"用户已解锁":"用户已锁定");
    }
}

